package com.koc.robot;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLES11;

import com.koc.robot.audio.MusicPlayer;
import com.koc.robot.audio.SoundPlayer;
import com.koc.robot.font.TrueTypeFont;

public class Global 
{
	public static int screenwidth = 1024;
	public static int screenheight = 768;
	
	public static int devicewidth = 0;
	public static int deviceheight = 0;
	
	public static Color fillcolor; 
	public static Color outlinecolor;
	
	public static float rainbowi = 0;
	
	public static Context context;
	public static KOCActivity main_activity;
	
	public static Texture scanlineimg = null;
	
	public static float creditss = 0;
	public static float creditsr = 0;
	
	// audio related
	public static MusicPlayer musicplayer = null;
	public static SoundPlayer soundplayer = null;
	
	public static float musicvolume = 0;
	
	public static float fadetimer = 0.0f;
	public static float fadecolor = 0.0f;
	public static float fadetimert = 1.0f;
	
	public static ArrayList<LevelData> leveldatas;
	
	public static int currentmap = 1;
	public static int unlockedlevels = 1;
	
	//rotate scene
	public static float ROTATE_SPEED = (float)Math.PI / 200.0f;
	public static float PITCH_SPEED = (float)Math.PI / 400.0f;
	
	public static TrueTypeFont menufont = null;
	public static TrueTypeFont levelselectfont = null;
	
	public static int blockVertexArrays[] = null;
	public static int blockVertexOffset = 0;
	public static FloatBuffer blockVertexBuffer = null;
	public static ShortBuffer blockIndexBuffer = null;
	public static int blockVertexBufferID = 0;
	public static int blockIndexBufferID = 0;
	
	public static Texture coinsbackimg = null;
	
	public static void initGlobal(Context _context)
	{
		fillcolor = new Color((float)(33 / 255.0f), (float)(189 / 255.0f), (float)(81 / 255.0f));
		outlinecolor = new Color((float)(21 / 255.0f), (float)(111 / 255.0f), (float)(48 / 255.0f));
		context = _context;
		
		scanlineimg = new Texture(context);
		scanlineimg.load("scanlines.png");
		scanlineimg.setFilter(FilterMode.FILTER_NEAREST, FilterMode.FILTER_NEAREST);
		
		musicplayer = new MusicPlayer(context);
		soundplayer = new SoundPlayer(context);
		
		rainbowi = (float)Math.random();
		
		TrueTypeFont.initFreeType();
		
		menufont = new TrueTypeFont();
		levelselectfont = new TrueTypeFont();
		try
		{
			menufont.load("font.ttf", 40);
			levelselectfont.load("font.ttf", 20);
		}
		catch (Exception e)
		{
		}
		
		coinsbackimg = new Texture(Global.context);
		coinsbackimg.load("coinsback.png");
	}
	
	public static void releaseGlobal()
	{
		TrueTypeFont.releaseFreeType();
	}
	
	public static BackgroundBlock[] backgroundblocks = null;
	
	public static Color getrainbowcolor(float i, float whiteness)
	{
		Color ret = new Color();
		if (i < (1.0f / 6.0f))
		{
			ret.R = 1.0f;
			ret.G = i * 6;
			ret.B = 0;
		}
		else if (i >= (1.0f / 6.0f) && (i < (2.0f / 6.0f)))
		{
			ret.R = (1.0f / 6.0f - (i - 1.0f / 6.0f)) * 6;
			ret.G = 1.0f;
			ret.B = 0;
		}
		else if (i >= (2.0f / 6.0f) && (i < (3.0f / 6.0f)))
		{
			ret.R = 0.0f;
			ret.G = 1.0f;
			ret.B = (i - 2.0f / 6.0f) * 6;
		}
		else if (i >= 3.0f / 6.0f && i < 4.0f / 6.0f)
		{
			ret.R = 0;
			ret.G = (1.0f / 6.0f - (i - 3.0f / 6.0f)) * 6.0f;
			ret.B = 1;
		}
		else if (i >= 4.0f / 6.0f && i < 5.0f / 6.0f)
		{
			ret.R = (i - 4.0f / 6.0f) * 6;
			ret.G = 0;
			ret.B = 1;
		}
		else
		{
			ret.R = 1;
			ret.G = 0;
			ret.B = (1.0f / 6.0f - (i - 5.0f / 6.0f)) * 6;
		}
		
		// whiteness
		ret.R *= whiteness;
		ret.G *= whiteness;
		ret.B *= whiteness;
		ret.A = 1.0f;
		
		return ret;
	}
	
	public static void fillblock(float x, float y, float width, float height, float r, float g, float b)
	{
		// now we begin to fill the buffer
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(x);
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(y);
		blockVertexArrays[blockVertexOffset++] = Util.toColor((int)(r * 255), (int)(g * 255), (int)(b * 255));
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(x);
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(y + height);
		blockVertexArrays[blockVertexOffset++] = Util.toColor((int)(r * 255), (int)(g * 255), (int)(b * 255));
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(x + width);
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(y + height);
		blockVertexArrays[blockVertexOffset++] = Util.toColor((int)(r * 255), (int)(g * 255), (int)(b * 255));
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(x + width);
		blockVertexArrays[blockVertexOffset++] = Float.floatToIntBits(y);
		blockVertexArrays[blockVertexOffset++] = Util.toColor((int)(r * 255), (int)(g * 255), (int)(b * 255));
	}
	
	public static void drawblock()
	{
		// first, fill the buffer
		IntBuffer buffer = IntBuffer.wrap(blockVertexArrays);
		buffer.position(0);
		GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, blockVertexBufferID);
		GLES11.glBufferSubData(GLES11.GL_ARRAY_BUFFER, 0, blockVertexOffset * 4, buffer);
		
		GLES11.glDisable(GLES11.GL_TEXTURE_2D);
		GLES11.glEnableClientState(GLES11.GL_VERTEX_ARRAY);
		GLES11.glDisableClientState(GLES11.GL_TEXTURE_COORD_ARRAY);
		GLES11.glEnableClientState(GLES11.GL_COLOR_ARRAY);
		
		GLES11.glBindBuffer(GLES11.GL_ELEMENT_ARRAY_BUFFER, blockIndexBufferID);
		GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, blockVertexBufferID);
		GLES11.glVertexPointer(2, GLES11.GL_FLOAT, 12, 0);
		GLES11.glColorPointer(4, GLES11.GL_UNSIGNED_BYTE, 12, 
				8);
		int drawVertNum = ((blockVertexOffset / 3) / 4) * 6;
		GLES11.glDrawElements(GLES11.GL_TRIANGLES, drawVertNum, GLES11.GL_UNSIGNED_SHORT, 0);
		
		GLES11.glBindBuffer(GLES11.GL_ELEMENT_ARRAY_BUFFER, 0);
		GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, 0);	
		GLES11.glEnable(GLES11.GL_TEXTURE_2D);
		GLES11.glEnableClientState(GLES11.GL_VERTEX_ARRAY);
		GLES11.glEnableClientState(GLES11.GL_TEXTURE_COORD_ARRAY);
		GLES11.glDisableClientState(GLES11.GL_COLOR_ARRAY);
	}
}
